package com.starryx.driverschool.controller;

import com.starryx.driverschool.po.Car;
import com.starryx.driverschool.po.Teacher;
import com.starryx.driverschool.service.CarService;
import com.starryx.driverschool.service.TeacherService;
import com.starryx.driverschool.vo.CarQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CarController {

    @Autowired
    private CarService carService;

    @GetMapping("/car")
    public String car(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
    Model model){
        model.addAttribute("page", carService.listCar(pageable));
        return "car";
    }

    @GetMapping("/car/input")
    public String input(Model model){
        model.addAttribute("car", new Car());
        return "car-input";
    }

    @GetMapping("/car/input/{id}")
    public String input(@PathVariable Long id, Model model){
        model.addAttribute("car", carService.getCar(id));
        return "car-input";
    }

    @PostMapping("/car/save")
    public String save(@Valid Car car,
                       BindingResult result,
                       RedirectAttributes attributes){
        Car car1 = carService.getCarByNo(car.getNo());
        if(car1 != null){
            result.rejectValue("no", "noError", "不能添加重复的车牌号码");
        }
        if (result.hasErrors()){
            return "car-input";
        }
        Car c = carService.saveCar(car);
        if(c == null){
            attributes.addFlashAttribute("message", "新增失败");
        }else{
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/car";
    }

    // 跳转到博客列表页面
    @PostMapping("/car/search")
    public String search(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
                         CarQuery carQuery,
                         Model model){
        model.addAttribute("page", carService.listCar(pageable, carQuery));
        return "car :: carList";       // 返回 blogList 片段，利用 thymeleaf 实现类似 ajax 那样的局部刷新
    }

    @GetMapping("/car/delete/{id}")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        carService.deleteCar(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/car";
    }

    @PostMapping("/car/batchdelete")
    public String batchDelete(@RequestParam List<Long> ids, RedirectAttributes attributes){
        for(Long id : ids){
            carService.deleteCar(id);
        }
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/car";
    }
}
